package com.situ.storehouse.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.situ.storehouse.dao.AttendantDao;
import com.situ.storehouse.entity.Attendant;
import com.situ.storehouse.util.StringDbutil;
import com.situ.storehouse.vo.ResultVO;

public class AttendantServiceImpl implements AttendantService {

	AttendantDao dao = new AttendantDao();

	
	@Override
	public void delete(Integer id) {
		dao.delete(id);

	}

	

	@Override
	public ResultVO select(Integer id) {
		ResultVO vo=new ResultVO();
		Attendant attendant=dao.select(id);
		if(attendant==null) {
			vo.setCode(1);
			vo.setMsg("该用户不存在,请刷新再试！");
		}else {
			vo.setData(attendant);
		}
		return vo;
		
	}

	

	public List<Attendant> select(Integer page,Integer limit,String keyword) {
		page=limit*(page-1);
		List<Attendant> list = dao.select(page,limit,keyword);
		return list;
	}

	@Override
	public long count(String keyword) {
		return dao.count(keyword);
		
	}

	@Override
	public Boolean check(String username, String password,HttpSession session) {
		Boolean check=dao.check(username,password,session);
		return check;
	}

	@Override
	public ResultVO save(Attendant attendant) {
		ResultVO vo=new ResultVO();
		if(attendant.getId()==null) {//新增
			if(!StringDbutil.isNoBlank(attendant.getUsername())) {
				vo.setCode(1);
				vo.setMsg("用户名不能为空！");
				return vo;
			}
			
			List<Attendant> attendants=dao.selectByUsername(attendant.getUsername());
			if(attendants.size()>0) {
				vo.setCode(1);
				vo.setMsg("用户名已经被使用！");
				return vo;
			}
			attendant.setPassword(StringDbutil.md5(attendant.getPassword()));//密码加密
			dao.add(attendant);
		}else {
			
			
			
			attendant.setId(attendant.getId());
			dao.update(attendant);
		}
		return vo;
	}

}
